namespace SinaFinance.ChartLibrary.AxisLibrary
{
    using SinaFinance;
    using SinaFinanceBaseClassLibrary;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Controls;

    public partial class KDayAxis : UserControl
    {
        //private bool _contentLoaded;
        private double bottomSpan = 30.0;
        public bool isDataReady = false;
        private const double KLineAxisHeightPercent = 0.66666666666666663;
        private List<KPoint> kLinePoints = new List<KPoint>();
        //internal Grid LayoutRoot;
        private double leftSpan = 85.0;
        private loading loadBar;
        private double rightSpan = 45.0;
        //internal Canvas rootCanvas;
        private string stockNumber = string.Empty;
        private double topSpan = 20.0;
        private const double VolumnAxisHeightPercent = 0.33333333333333331;
        private List<string> volumnYAxisTips = new List<string>();
        private List<string> xAxisTips = new List<string>();
        private List<string> yAxisTips = new List<string>();

        public KDayAxis()
        {
            this.InitializeComponent();
            base.Loaded += new RoutedEventHandler(this.KDayAxis_Loaded);
        }

        private void AddLoadingToCanvas()
        {
            this.loadBar = new loading();
            this.loadBar.Margin = new Thickness(base.Width / 2.0, (base.Height / 2.0) - (this.loadBar.Height / 2.0), 0.0, 0.0);
            this.rootCanvas.Children.Add(this.loadBar);
        }

        protected void DataBinding()
        {
            this.isDataReady = false;
            try
            {
                DB_SinaDataHelp help = new DB_SinaDataHelp();
                help.GetKLineDataCompleted += new GetKLineDataHelp(this.Dsdh_GetKLineDataCompleted);
                if (this.stockNumber.Length.Equals(6))
                {
                    help.GetKLineData(this.stockNumber);
                }
                else
                {
                    help.GetKLineData(this.stockNumber.Substring(2, 6));
                }
            }
            catch
            {
            }
        }

        private void Dsdh_GetKLineDataCompleted(DB_KLine _DB_KLine)
        {
            if (_DB_KLine != null)
            {
                DB_Transition.DB_KDay(_DB_KLine, out this.kLinePoints, out this.xAxisTips, out this.yAxisTips, out this.volumnYAxisTips);
                this.RenderKLineControl();
                this.RenderVolumnControl();
                this.isDataReady = true;
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/ChartLibrary/AxisLibrary/KDayAxis.xaml", UriKind.Relative));
        //        this.LayoutRoot = (Grid) base.FindName("LayoutRoot");
        //        this.rootCanvas = (Canvas) base.FindName("rootCanvas");
        //    }
        //}

        protected void InitXaxisTips()
        {
            int num = 0;
            IEnumerator enumerator = (IEnumerator) this.xAxisTips.GetEnumerator();
            List<string> list = new List<string>();
            while (enumerator.MoveNext())
            {
                if (num.Equals(0) || ((num % 10)).Equals(9))
                {
                    list.Add(enumerator.Current.ToString());
                }
                num++;
            }
            this.xAxisTips.Clear();
            this.xAxisTips = list;
        }

        private void KDayAxis_Loaded(object sender, RoutedEventArgs e)
        {
            if (base.Width <= 0.0)
            {
                base.Width = 600.0;
            }
            if (base.Height <= 0.0)
            {
                base.Height = 400.0;
            }
            this.rootCanvas.Width = base.Width;
            this.rootCanvas.Height = base.Height;
            this.AddLoadingToCanvas();
            this.DataBinding();
        }

        private void RenderKLineControl()
        {
            this.InitXaxisTips();
            KLineAxis axis = new KLineAxis();
            double num = base.Height * 0.66666666666666663;
            double width = base.Width;
            axis.Width = width;
            axis.Height = num;
            axis.TopSpan = this.topSpan;
            axis.BottomSpan = this.bottomSpan;
            axis.LeftSpan = this.leftSpan;
            axis.RightSpan = this.rightSpan;
            axis.XAxisTips = this.xAxisTips;
            axis.YAxisTips = this.yAxisTips;
            axis.KLinePoints = this.kLinePoints;
            this.rootCanvas.Children.Add(axis);
        }

        private void RenderVolumnControl()
        {
            List<TimeSharingPoint> list = new List<TimeSharingPoint>();
            IEnumerator enumerator = (IEnumerator) this.kLinePoints.GetEnumerator();
            List<bool> list2 = new List<bool>();
            while (enumerator.MoveNext())
            {
                KPoint point = (KPoint) enumerator.Current;
                TimeSharingPoint point2 = new TimeSharingPoint {
                    VolumnInfo = point.VolumnInfo
                };
                if (Convert.ToDouble(point.DayStartPrice) > Convert.ToDouble(point.DayEndPrice))
                {
                    list2.Add(false);
                }
                else
                {
                    list2.Add(true);
                }
                list.Add(point2);
            }
            VolumnAxis axis = new VolumnAxis {
                ColumnTypeEnum = ColumnTypeEnum.KLine
            };
            double num = base.Height * 0.33333333333333331;
            double width = base.Width;
            axis.Width = width;
            axis.Height = num;
            axis.TopSpan = this.topSpan;
            axis.BottomSpan = this.bottomSpan;
            axis.LeftSpan = this.leftSpan;
            axis.RightSpan = this.rightSpan;
            axis.YaxisDatalist = this.volumnYAxisTips;
            axis.TimeSharingPoints = list;
            axis.Def_COULUMNSCOUNT = 90;
            axis.Def_XAXISMARKCOUNT = 8;
            axis.Def_YAXISMARKCOUNT = 5;
            axis.VolumnFlagList = list2;
            this.rootCanvas.Children.Add(axis);
            axis.Margin = new Thickness(0.0, base.Height * 0.66666666666666663, 0.0, 0.0);
        }

        public string StockNumber
        {
            get
            {
                return this.stockNumber;
            }
            set
            {
                this.stockNumber = value;
            }
        }
    }
}
